package com.ygh.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ygh.mapper.AdminUserMapper;
import com.ygh.pojo.AdminUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.UUID;

@Service
public class AdminUserServiceImpl implements AdminUserService{
    @Autowired
    private AdminUserMapper adminUserMapper;


    @Override
    public String getadminUser(AdminUser adminUser) {
        /**
         * MD5进行密码加密
         * 查询数据库用户是否存在  存在生成UUID返回前端
         * 不存在返回null  校验失败
         * */
        String md5 = DigestUtils.md5DigestAsHex(adminUser.getAdminPassword().getBytes());
        adminUser.setAdminPassword(md5);
        QueryWrapper<AdminUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("admin_name", adminUser.getAdminName()).eq("admin_password", adminUser.getAdminPassword());
        Integer count = adminUserMapper.selectCount(queryWrapper);
        if(count==0){
            return null;
        }
        /**登录成功记录登录时间*/
        QueryWrapper<AdminUser> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("admin_name", adminUser.getAdminName());
        adminUserMapper.update(adminUser.setLoginTime(new Date()),queryWrapper1);
        /**返回UUID  token*/
        String token = UUID.randomUUID().toString().replace("-", "");
        return token;

    }
}
